package stack.be.touch.ui;

import greendroid.app.GDActivity;
import greendroid.graphics.drawable.ActionBarDrawable;
import greendroid.widget.NormalActionBarItem;
import greendroid.widget.ActionBarItem.Type;

import java.util.ArrayList;

import stack.be.touch.R;
import stack.be.touch.adapter.SitesAdapter;
import stack.be.touch.dao.SiteDAO;
import stack.be.touch.dto.SiteDTO;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends GDActivity {
	
	private final static int REQUEST_PICK_SITES = 1;
	
	private SiteDAO mSiteDAO;
	private Cursor mSites;
	private SitesAdapter sitesAdpt;
	private ArrayList<SiteDTO> mySites;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActionBarContentView(R.layout.main);
        
        addActionBarItem(getActionBar()
                .newActionBarItem(NormalActionBarItem.class)
                .setDrawable(new ActionBarDrawable(getResources(), R.drawable.ic_action_bar_info)), R.id.action_bar_view_info);
        
        mSiteDAO = new SiteDAO(MainActivity.this);
        mSites = mSiteDAO.getMySites(SiteDTO.TABLE_NAME);
        startManagingCursor(mSites);
        mSites.moveToFirst();
        
        // populate data to array list
//        int i = 0;
        mySites = new ArrayList<SiteDTO>();
        while(!mSites.isAfterLast()){
//        	MySite site = new MySite(mSiteDAO.getEndpoint(mSites), mSiteDAO.getName(mSites), 0, mSiteDAO.getPosition(mSites), null);
        	if (mSiteDAO.getChecked(mSites) == 1){
	        	SiteDTO site = new SiteDTO(mSiteDAO.getName(mSites), mSiteDAO.getEndpoint(mSites),mSiteDAO.getPosition(mSites), 1);
//	        	mySites.add(i,site);
	        	mySites.add(site);
        	}
//        	i++;
        	mSites.moveToNext();
        }
        sitesAdpt = new SitesAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mySites);
        GridView gv = (GridView)findViewById(R.id.grid);
        gv.setAdapter(sitesAdpt);
        
        gv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				SiteDTO tmpSite = (SiteDTO)sitesAdpt.getItem(position);
				String endpoint = tmpSite.getEndpoint();
				String name = tmpSite.getName();
				String uri = "betouch://questions/all" +
						"?endpoint=" + Uri.encode(endpoint) +
						"&name=" + Uri.encode(name);
				Intent i = new Intent(MainActivity.this,QuestionsPage.class);
				i.setData(Uri.parse(uri));
				startActivity(i);
				
			}
		});
        
        
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.option, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.pickSites) {
			Intent intent = new Intent(MainActivity.this, PickSitesActivity.class);
			startActivityForResult(intent, REQUEST_PICK_SITES);
			return true;
		}
		return false;
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	switch (requestCode) {
		case REQUEST_PICK_SITES:
			if(resultCode == RESULT_OK)
				mSites.requery();
			startManagingCursor(mSites);
	        mSites.moveToFirst();
	        
	        // populate data to array list
//	        int i = 0;
	        mySites.clear();
	        while(!mSites.isAfterLast()){
//	        	MySite site = new MySite(mSiteDAO.getEndpoint(mSites), mSiteDAO.getName(mSites), 0, mSiteDAO.getPosition(mSites), null);
//	        	MySites.add(i,site);
//	        	i++;
//	        	mSites.moveToNext();
	        	if (mSiteDAO.getChecked(mSites) == 1 ){
		        	SiteDTO site = new SiteDTO(mSiteDAO.getName(mSites), mSiteDAO.getEndpoint(mSites),mSiteDAO.getPosition(mSites), mSiteDAO.getChecked(mSites));
//		        	mySites.add(i,site);
		        	mySites.add(site);
	        	}
//	        	i++;
	        	mSites.moveToNext();
	        }
			sitesAdpt.notifyDataSetChanged();
			break;

		default:
			
			super.onActivityResult(requestCode, resultCode, data);
			break;
        }
			
		}
    	
    	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}// CLASS


















